package com.elinkway.infinitemovies.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.text.Html;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.ui.activity.DownloadActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.ui.activity.play.PlayLiveController;
import com.elinkway.infinitemovies.ui.activity.play.PlayMediaController;
import com.le123.ysdq.R;

/**
 * 公共的Dialog工具类
 *
 * @author zhangshuo
 */
public class DialogUtil {
    public static Builder mCheckNetBuilder;
    public static DialogUtil dialogUtil;
    private Activity mContext = null;
    private Dialog mDialog = null;
    private static AlertDialog alertDialog;


    // wuxinrong deleted begin
//	public DialogUtil(Context context) {
//		this.mContext = context;
//		mCheckNetBuilder = new Builder(context);
//	}

//	public DialogUtil(){}
    // wuxinrong deleted end

    public static DialogUtil getInstance(Activity context) {
        if (null == dialogUtil) {
            dialogUtil = new DialogUtil();
        }
        mCheckNetBuilder = new Builder(context);
        return dialogUtil;
    }

    /**
     * @param context
     * @param id
     * @param isCanCancelTouchOutSide 点击外围是否消失
     * @param dialogOnClickListener
     * @param dialogOnKeyListener     zhangshuo
     *                                2014年6月10日 下午10:32:50
     */
    public static void createSingBtnDialog(Context context, int id,
                                           boolean isCanCancelTouchOutSide, OnClickListener dialogOnClickListener,
                                           OnKeyListener dialogOnKeyListener) {
        Builder singleBtnBuilder = new Builder(context);
        singleBtnBuilder.setTitle(R.string.tip);
        singleBtnBuilder.setMessage(id);
        singleBtnBuilder.setPositiveButton(R.string.ok, dialogOnClickListener);
        singleBtnBuilder.setOnKeyListener(dialogOnKeyListener);
        AlertDialog alertDialog = singleBtnBuilder.create();
        if (alertDialog != null && !alertDialog.isShowing()) {
            alertDialog.setCanceledOnTouchOutside(isCanCancelTouchOutSide);
            alertDialog.show();
        }
    }

    /**
     * 检测sd卡容量对话框
     *
     * @author daipei
     */
    public void checkSDCardSpaceDialog() {
        mCheckNetBuilder.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MoviesApplication.getInstance(), DownloadActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        MoviesApplication.getInstance().startActivity(intent);
                        dialog.dismiss();
                    }
                });

        mCheckNetBuilder.setTitle(R.string.tip);
        mCheckNetBuilder.setMessage(R.string.sdcard_nospace);
        mCheckNetBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getRepeatCount() == 0) {
                    dialog.dismiss();
                }
                return true;
            }
        });

        mCheckNetBuilder.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        try {
            if (null == alertDialog) {
                alertDialog = mCheckNetBuilder.create();
            }
            if (alertDialog != null && !alertDialog.isShowing()) {
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void checkMobileNetDialog(final VideoDetailActivity detailActivity, final int position, final boolean isGrid) {
        mCheckNetBuilder.setPositiveButton(R.string.string_continue,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        detailActivity.setWifiTo3GFlag(true);
//                        detailActivity.IntentToPlayActivitySelfOwn();
                        if(detailActivity.mPlaySrcList != null && detailActivity.mPlaySrcList.getPlaySrcList() != null &&
                                detailActivity.mPlaySrcList.getPlaySrcList().size() > 0 && "2".equals(detailActivity.mPlaySrcList.getPlaySrcList().get(0).getOpenway())) {
                            detailActivity.jumpToWebViewPlay(position);
                        }else {
                            detailActivity.playVideo(position, isGrid);
                        }
                        dialog.dismiss();
                    }
                });

        mCheckNetBuilder.setTitle(R.string.tip);
        mCheckNetBuilder.setMessage(R.string.moblie_net_tip);
        mCheckNetBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getRepeatCount() == 0) {
                    detailActivity.setWifiTo3GFlag(false);
                    dialog.dismiss();
                }
                return true;
            }
        });

        mCheckNetBuilder.setNegativeButton(R.string.exit,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        detailActivity.setWifiTo3GFlag(false);
                        dialog.dismiss();
                    }
                });

        AlertDialog alertDialog = mCheckNetBuilder.create();
        if (alertDialog != null && !alertDialog.isShowing()) {
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        }
    }

    /**
     * 播放器弹出网络提示框
     *
     * @author daiyanfeng
     */
    public void showPlayerCheckNetDialog(final PlayMediaController playMediaController, boolean isCouldCancel,
                                         boolean isCanceledOnTouchOutside, int strId) {
        if (null == mContext || null == mCheckNetBuilder) {
            return;
        }
        mCheckNetBuilder.setTitle(R.string.tip);
        // 设置提示语内容
        mCheckNetBuilder.setMessage(strId);
        mCheckNetBuilder.setPositiveButton(R.string.string_continue,
                new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (null != mDialog) {
                            mDialog.dismiss();
                        }
//                        playMediaController.getmSelfOwnActivity().setWifiTo3GFlag(true);
                        // 用户点击确定按钮
                        if (playMediaController.ismIsPause())
                            playMediaController.clickPauseOrPlay();
                    }
                });
        mCheckNetBuilder.setNegativeButton(R.string.exit,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        playMediaController.getmSelfOwnActivity().finish();
                    }
                });

        if (null == mDialog) {
            mDialog = mCheckNetBuilder.create();
            mDialog.setCancelable(isCouldCancel);
            mDialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside);
            mDialog.setOnKeyListener(new DialogOnKeyListener());
        }

        if (!mDialog.isShowing()) {
            try {
                mDialog.show();
            } catch (Exception e) {
                mCheckNetBuilder = null;
                mDialog = null;
                e.printStackTrace();
            }
        }
    }

    /**
     * 播放器弹出网络提示框(全屏)
     *
     * @author liuxiaojiang
     */
    public void showFullScreenDialog(final PlayMediaController playMediaController, String tip) {
        if (null == mContext) {
            return;
        }
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = inflater.inflate(R.layout.dialog_fullscreen_checknet, null);
        final Dialog fullDialog = new Dialog(mContext, R.style.noframe_dialog);
        fullDialog.setContentView(dialogView);
        TextView messageTv = (TextView) dialogView.findViewById(R.id.message_tv);
        messageTv.setText(Html.fromHtml(tip));
        dialogView.findViewById(R.id.continue_play_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != fullDialog) {
                    fullDialog.dismiss();
                }
//                playMediaController.getmSelfOwnActivity().setWifiTo3GFlag(true);
                // 用户点击确定按钮
                if (playMediaController.ismIsPause())
                    playMediaController.clickPauseOrPlay();
            }
        });
        dialogView.findViewById(R.id.exit_play_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != fullDialog) {
                    fullDialog.dismiss();
                }
                playMediaController.getmSelfOwnActivity().finish();
            }
        });

        if (!fullDialog.isShowing()) {
            try {
                fullDialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showPlayerCheckNetDialog(final PlayLiveController liveController,
                                         boolean isCouldCancel, boolean isCanceledOnTouchOutside, int strId) {
        if (null == mContext || null == mCheckNetBuilder) {
            return;
        }
        mCheckNetBuilder.setTitle(R.string.tip);
        // 设置提示语内容
        mCheckNetBuilder.setMessage(strId);
        mCheckNetBuilder.setPositiveButton(R.string.string_continue, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (null != mDialog) {
                    mDialog.dismiss();
                }
                liveController.getmActivityLive().setWifiTo3GFlag(true);
                liveController.continePlayLvie();
            }
        });
        mCheckNetBuilder.setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                liveController.getmActivityLive().finish();
            }
        });

        if (null == mDialog) {
            mDialog = mCheckNetBuilder.create();
            mDialog.setCancelable(isCouldCancel);
            mDialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside);
            mDialog.setOnKeyListener(new DialogOnKeyListener());
        }

        if (!mDialog.isShowing()) {
            try {
                mDialog.show();
            } catch (Exception e) {
                mCheckNetBuilder = null;
                mDialog = null;
                e.printStackTrace();
            }
        }
    }

    private class DialogOnKeyListener implements OnKeyListener {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return false;
            }
            return false;
        }
    }

    /**
     * liteApp下载网络提示框
     */
    public void liteAPPDownNetDialog(final Context context) {
        mCheckNetBuilder.setPositiveButton(R.string.string_continue,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        LiteAppManager.getInstance().excuteDownLiteAppLogic(context);
                    }
                });

        mCheckNetBuilder.setTitle(R.string.tip);
        mCheckNetBuilder.setMessage(R.string.liteapp_moblie_net_tip);
        mCheckNetBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getRepeatCount() == 0) {
                    dialog.dismiss();
                }
                return true;
            }
        });

        mCheckNetBuilder.setNegativeButton(R.string.exit,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        AlertDialog alertDialog = mCheckNetBuilder.create();
        if (alertDialog != null && !alertDialog.isShowing()) {
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        }
    }

}
